﻿using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace ProjectNocturnalFool
{
    class EntitySprite
    {
        #region Fields
        public int entityIndex;
        public List<SpriteAnimation> componentSprites { get; set; }
        public Point onTile { get { return GameLogic.Instance.gameBoardLogic.entities.allEntities[entityIndex].onTile; } set { ;} }
        public Vector2 drawOffset = Vector2.Zero;

        Vector2 position {
            get {
                float xPos = (onTile.X) * TileImage.tileImageStepX;
                float yPos = (onTile.Y) * TileImage.tileImageStepY;
                if(Math.Abs(onTile.Y)%2==1){ xPos+=TileImage.oddRowXOffset; }
                xPos += TileImage.tileImageStepX / 2;
                yPos += TileImage.tileImageStepY / 2 - 
                    (TileImage.tileImageStepY * 2 * GameLogic.Instance.gameBoardLogic.tiles.getAltitude(onTile.X, onTile.Y));//altitude
                return new Vector2(
                   xPos, yPos
                );
            }
            set { position = value;} 
            }
        #endregion

        #region Constructor
        public EntitySprite(int _entityIndex) {
            entityIndex = _entityIndex;
            componentSprites = new List<SpriteAnimation>();
            componentSprites.Add(new SpriteAnimation(TacticsGame.Instance.Content.Load<Texture2D>("Entities/vladSprite")));
            componentSprites.Add(new SpriteAnimation(TacticsGame.Instance.Content.Load<Texture2D>("Entities/vladSprite")));
            componentSprites.Add(new SpriteAnimation(TacticsGame.Instance.Content.Load<Texture2D>("Entities/vladSprite")));
            
            for (int i=0; i < componentSprites.Count; i++) {
                componentSprites[i].AddAnimation("AdamWest", 0, 
                    Constants.medSpriteHeight * Constants.walkLeftRowIndex + (Constants.esComponentHeight*(i)), 
                    Constants.medSpriteWidth, 
                    Constants.esComponentHeight,
                    Constants.walkLeftFrameNumber, 
                    0.2f);

                componentSprites[i].Position = position;
                componentSprites[i].CurrentAnimation = "AdamWest";
                componentSprites[i].IsAnimating = true;
                componentSprites[i].DrawOffset = drawOffset;
           
            }
           

        }
        #endregion
        #region Methods
        public bool CheckForMouseHover(Vector2 hloc) {
            for (int i = 0; i < componentSprites.Count; i++) {
                if (componentSprites[i].hoveredOn) {
                    return true;
                }
            }
            return false;
        }

        #endregion
        #region Draw
        public void Draw(GameTime gameTime) {
            for (int i = 0; i < componentSprites.Count; i++) {
                int currentAltitude = GameLogic.Instance.gameBoardLogic.tiles.getAltitude(onTile.X, onTile.Y);
                
               
                componentSprites[i].Draw(TacticsGame.Instance.spriteBatch, 0, 0);
                componentSprites[i].DrawDepth = GameLogic.Instance.gameBoardLogic.tiles.getDrawDepth(onTile.Y, onTile.X, currentAltitude + 1);
            }
        }

        #endregion
        #region Update

        public void Update(GameTime gameTime) {
            for (int i = 0; i < componentSprites.Count; i++)
            {
                componentSprites[i].Update(gameTime);
                componentSprites[i].Position = position -
                    new Vector2(Constants.medSpriteCentreX * Camera.scale, Constants.medSpriteCentreY * Camera.scale) + //centering logic
                    new Vector2(0, Constants.esComponentHeight * i * Camera.scale);
            }
       }

        #endregion
    }
}
